package com.example.learn.service;

import com.example.learn.config.LogConfig;
import com.example.learn.entity.MemorandumForm;
import com.example.learn.entity.User;
import com.example.learn.mapper.MemorandumFormMapper;
import com.example.learn.utile.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class MemorandumFormService {
    private static final Logger LOG = LoggerFactory.getLogger(MemorandumFormService.class);
    @Autowired
    MemorandumFormMapper memorandumFormMapper;
    public String adddmemorandum(MemorandumForm memorandumForm, User user) {
        memorandumForm.setUseracount(user.getName());
        memorandumForm.setId(UUID.randomUUID().toString());
        memorandumForm.setCreateTime(TimeUtil.getCurrentTime());
        memorandumFormMapper.insert(memorandumForm);
       return  "ok";
    }

    public List<MemorandumForm> getmemorandum(MemorandumForm memorandumForm) {
        List<MemorandumForm> memorandumFormList=  memorandumFormMapper.getmemorandum(memorandumForm);
        for (MemorandumForm memorandumForm1:
        memorandumFormList) {
            if(memorandumForm1.getImportance()){
                memorandumForm1.setColor("red");
            }else {
                memorandumForm1.setColor("blue");
            }
            if(memorandumForm1.getIsfinish()=="1"){
                memorandumForm1.setColorFinish("blue");
                memorandumForm1.setColor("blue");
            }else {
                memorandumForm1.setColorFinish("red");
            }
            long  tian=   TimeUtil.between_days(new Date(),memorandumForm1.getEnddate());
            memorandumForm1.setFinishDay(tian);
        }
        return  memorandumFormList;
    }
     @Transactional
    public String updatememorandum(String isfinish,String id, User user) {
        MemorandumForm memorandumForm=   memorandumFormMapper.selectByPrimaryKey(id);
        memorandumForm.setIsfinish(isfinish);
        if(memorandumForm!=null){
            memorandumFormMapper.updateByPrimaryKey(memorandumForm);
        }
        return  "ok";
    }
}
